package org.jeecg.modules.fk.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;


import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basesource.entity.FpCjDto;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.fk.entity.dto.AuthOrgCountDTO;
import org.jeecg.modules.fk.entity.req.DjUserAssignRequest;
import org.jeecg.modules.fk.entity.vo.AuthOrgCountVO;
import org.jeecg.modules.fk.mapper.DjOrgMapper;
import org.jeecg.modules.fk.service.IDjOrgService;
import org.jeecg.modules.fk.utils.CheckRoleUtil;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

/**
 *
 */
@Service
public class DjOrgServiceImpl extends MppServiceImpl<DjOrgMapper, DjOrg> implements IDjOrgService {
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public List<DjOrg> listForPage(DjOrg djOrg) {
        return listForPageWithOrder(djOrg, null, null);
    }

    @Override
    public List<DjOrg> listForPageWithOrder(DjOrg djOrg, String orderColumn, String orderType) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
        //判断是否为部门领导，若是则添加部门前缀作为查询条件
        if (CheckRoleUtil.checkIsDeptAdmin(roleNameListString)) {
            //省行，分支行，网点领导
            djOrg.setKeyWord(loginUser.getOrgCode());
        } else if (!CheckRoleUtil.checkIsSysAdmin(roleNameListString)) {
            //不是部门领导，也不是管理员，则只能查询管户
            djOrg.setManageUserId(loginUser.getId());
        }
        return baseMapper.listForPage(djOrg, orderColumn, orderType);
    }

    @Override
    public IPage<DjOrg> page2(Page<DjOrg> page, @Param("djOrg")DjOrg djOrg, String orderColumn, String orderType) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
        //判断是否为部门领导，若是则添加部门前缀作为查询条件
        if (CheckRoleUtil.checkIsDeptAdmin(roleNameListString)) {
            //省行，分支行，网点领导
            djOrg.setKeyWord(loginUser.getOrgCode());
        } else if (!CheckRoleUtil.checkIsSysAdmin(roleNameListString)) {
            //不是部门领导，也不是管理员，则只能查询管户
            djOrg.setManageUserId(loginUser.getId());
        }
        return baseMapper.selectPage2(page, djOrg, orderColumn, orderType);
    }

    @Override
    public void setDjUser2SysUser(DjUserAssignRequest req) {
        LambdaUpdateWrapper<DjOrg> update = Wrappers.<DjOrg>lambdaUpdate();
        update.set(DjOrg::getManageUserId, req.getSysUserId())
                .eq(DjOrg::getNsrsbh, req.getNsrsbh())
                .eq(DjOrg::getOrgId, req.getOrgId());
        update(update);
    }

    @Override
    public AuthOrgCountVO authOrgCount(AuthOrgCountDTO dto) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
        DjOrg djOrg = new DjOrg();

        //判断是否为部门领导，若是则添加部门前缀作为查询条件
        if (CheckRoleUtil.checkIsDeptAdmin(roleNameListString)) {
            djOrg.setSaleNetCode(loginUser.getOrgCode());
        } else if (!CheckRoleUtil.checkIsSysAdmin(roleNameListString)) {
            //不是部门领导，也不是管理员，则只能查询管户
            djOrg.setManageUserId(loginUser.getId());
        }
        if (dto != null) {
            if (StringUtils.hasLength(dto.getSaleNetCode())) {
                djOrg.setSaleNetCode(dto.getSaleNetCode());
            }
            djOrg.setNsrmc(dto.getNsrmc());
            djOrg.setIsAuth(dto.getIsAuth());
        }
        return baseMapper.authOrgCount(djOrg);
    }

    @Override
    public DjOrg getByNsrsbhOrShxydm(String nsrsbhOrShxydm) {
        LambdaQueryWrapper<DjOrg> wrapper = Wrappers.lambdaQuery(DjOrg.class);
        wrapper.eq(DjOrg::getNsrsbh, nsrsbhOrShxydm).or().eq(DjOrg::getShxydm, nsrsbhOrShxydm);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public List<DjOrg> incrementListByShardTotal(FpCjDto dto) {
        return baseMapper.incrementListByShardTotal(dto);
    }

    @Override
    public List<DjOrg> fullDoseListByShardTotal(FpCjDto dto) {
        return baseMapper.fullDoseListByShardTotal(dto);
    }

    @Override
    public void updateDjorgById(DjOrg djOrg) {
        baseMapper.updateDjorgById(djOrg);
    }
}
